<?php

namespace Business;

class Article extends Base {

	public function saveArticle($articleId, $userId, $categoryId, $title, $content) {
		$content = $this->stripContent($content);
		$title = trim($title);
		if (!$categoryId) {
			$this->throwException('请选择分类！');
		}
		if (!$title) {
			$this->throwException('请填写标题！');
		}
		if (strlen($title) > 128) {
			$this->throwException('标题太长了！');
		}
		$this->checkContent($content);
		$pq = \phpQuery::newDocument($content);
		$imgFist = $pq->find('img');
		$cover = '';
		$imgList = '';
		if ($imgFist->length) {
			$i = 0;
			$imgFist->each(function($o) use (& $i, & $imgList, & $cover) {
				$img = pq($o);
				$src = $img->attr('src');
				$src = trim($src);
				if (!$i) {
					$cover = $src;
				}
				$imgList .= ',' . $src;
				$i++;
			});
		}
		$imgList = ltrim($imgList, ',');
		$articleId = \Data\Article::instance()->saveArticle($articleId, $userId, $categoryId, $title, $content, $cover, $imgList);
		return $articleId;
	}

	public function findArticle($pager, $getContent = false, $categoryId = 0) {
		$result = \Data\Article::instance()->findArticle($pager, $getContent, $categoryId);
		$userIds = array();
		foreach ($result as $val) {
			$userIds[$val['UserId']] = $val['UserId'];
		}
		$users = \Data\User::instance()->getUserByIds($userIds);
		foreach ($result as $k => $v) {
			$userId = $v['UserId'];
			$result[$k]['UserInfo'] = array();
			if (!empty($users[$userId])) {
				$result[$k]['UserInfo'] = $users[$userId];
			}
		}
		return $result;
	}

	public function getRightArticles($limit = 5) {
		$result = \Data\Article::instance()->getRightArticles($limit);
		$articleIds = array();
		foreach ($result as $val) {
			$articleIds[] = $val['ArticleId'];
		}
		$idToNumbers = array();
		if ($articleIds) {
			$idToNumbers = \Data\Comments::instance()->getCountByIds($articleIds);
		}
		foreach ($result as & $val) {
			$val['CommentsNumber'] = empty($idToNumbers[$val['ArticleId']]) ? 0 : (int) $idToNumbers[$val['ArticleId']];
		}
		return $result;
	}

	public function findArticleByUserId($pager, $userId, $categoryId = 0) {
		$result = \Data\Article::instance()->findArticleByUserId($pager, $userId, $categoryId);
		return $result;
	}

    public function getUserArticleCountByUserId($userId, $categoryId = 0) {
        $result = \Data\Article::instance()->getUserArticleCountByUserId($userId, $categoryId);
        return $result;
    }

	public function getDetailById($articleId, $inc = true) {
		$result = \Data\Article::instance()->getDetailById($articleId);
		if ($result && $inc) {
			\Data\Article::instance()->increaseView($articleId);
		}
		return $result;
	}

	public function getUserOtherArticles($userId, $articleId, $limit = 5) {
		$result = \Data\Article::instance()->getUserOtherArticles($userId, $articleId, $limit);
		return $result;
	}

	public function getHasArticleCategory($userId) {
		$categoryIdToNumber = \Data\Article::instance()->categoryArticleNumberByUserId($userId);
		if (!$categoryIdToNumber) {
			return array();
		}
		$categoryIds = array_keys($categoryIdToNumber);
		$result = \Data\Category::instance()->getParentInfoByIds($categoryIds);
		foreach ($result as & $val) {
			foreach ($val['List'] as & $v) {
				$v['Number'] = $categoryIdToNumber[$v['CategoryId']];
			}
		}
		return $result;
	}

	public function addComment($userId, $articleId, $content) {
		$content = $this->stripContent($content);
		$len = mb_strlen($content);
		if (!$content || $len < 4) {
			$this->throwException('评论太短了！');
		}
		if ($len > 1500) {
			$this->throwException('评论太长了！');
		}
		\Data\Comments::instance()->addComment($userId, $articleId, $content);
	}

	public function getCommentsByArticleId($articleId, $pager) {
		$result = \Data\Comments::instance()->getCommentsByArticleId($articleId, $pager);
		return $result;
	}

    public function del($articleId, $userId) {
        \Data\Article::instance()->delByIdAndUserId($articleId, $userId);
    }

	private function checkContent($content) {
		$pq = \phpQuery::newDocument($content);
		$imgFist = $pq->find('img:fist');
		$content = strip_tags($content);
		$content = trim($content);
		$length = strlen($content);
		if (!$length && !$imgFist->length) {
			$this->throwException('请输入内容!');
		}
		if ($length > 10240) {
			$this->throwException('内容太长了！');
		}
	}

	private function stripContent($content) {
		$content = strip_tags($content, '<p><u><span><img><br>');
		$content = trim($content);
		return $content;
	}
}